package com.servlet;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.config.AlipayConfig;
import com.entity.Foods;
import com.entity.Orders;
import com.service.FoodsService;
import com.service.OrderService;
import com.service.impl.FoodsServiceImpl;
import com.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@WebServlet("/Order.let")
public class OrderServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        //1.设置编码
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        //2.获取各种对象
//        PrintWriter out = resp.getWriter();
        OrderService orderService = new OrderServiceImpl();
        String type = req.getParameter("type");
        switch (type){
            case "listOrderById":
                String id = req.getParameter("userId");
                List<Orders> orders = orderService.listALLOrder(Integer.parseInt(id));
                req.setAttribute("orders",orders);
                req.getRequestDispatcher("checkout.jsp").forward(req,resp);
                break;
            case "orderPay":
                Integer size = Integer.parseInt(req.getParameter("size"));
                Integer userId = Integer.parseInt(req.getParameter("userId"));
                Orders orders1 = orderService.OrderPay(userId);
                req.setAttribute("orders1",orders1);
                if(size<800){
                    req.getRequestDispatcher("phonePay.jsp").forward(req,resp);
                }else {
                    req.getRequestDispatcher("pay.jsp").forward(req,resp);
                }
                break;
            case "queryOrderDetail":
                String oNumber=req.getParameter("oNumber");
                List<Map<String,Object>> foodsDetail=orderService.queryOrderDetail(oNumber);
                System.out.println(oNumber);
                System.out.println(foodsDetail);
                req.setAttribute("foodsDetail",foodsDetail);
                req.getRequestDispatcher("/PersonalCenter/foodsDetail.jsp").forward(req,resp);
                break;
            case "centerPay":
                oNumber=req.getParameter("oNumber");
                orders1=orderService.centerPay(oNumber);
                req.setAttribute("orders1",orders1);
                req.getRequestDispatcher("pay.jsp").forward(req,resp);
            case "bGOrderAll":
                orders=orderService.queryOrderAll();
                req.setAttribute("BgOrders",orders);
                req.getRequestDispatcher("/bgFoodKa/detail/orderList.jsp").forward(req,resp);
                break;
            case "bGQueryOrderDetail":
                oNumber=req.getParameter("oNumber");
                List<Map<String,Object>> bGFoodsDetail=orderService.queryOrderDetail(oNumber);
                System.out.println(oNumber);
                System.out.println(bGFoodsDetail);
                req.setAttribute("bGFoodsDetail",bGFoodsDetail);
                req.getRequestDispatcher("/bgFoodKa/detail/orderDetail.jsp").forward(req,resp);
                break;
        }
    }
}
